package com.example.backend.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.backend.common.EncryptUtil;
import com.example.backend.entity.DesignProduct;
import com.example.backend.entity.UserCard;
import com.example.backend.mapper.DesignProductMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
public class DesignProductController {
    @Resource
    DesignProductMapper designProductMapper;
    //Set<Map.Entry<String, List<DesignProduct>>>
    @RequestMapping(value ="/stylesheet",method = GET)
    String getDiyStyleSheet(@RequestParam(value="type",required=true) int type) {
        QueryWrapper<DesignProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        List<DesignProduct> users = designProductMapper.selectList(wrapper);
        Map<String, List<DesignProduct>> stringListMap =
                users.stream().sorted(Comparator.comparing(DesignProduct::getId)).collect(Collectors.groupingBy(DesignProduct::getCategory));
//        Set<Map.Entry<String, List<DesignProduct>>> entries = stringListMap.entrySet();
        ObjectMapper mapper = new ObjectMapper();
        String result = null;
        try {
            System.out.println(Convert.toStr(mapper.writeValueAsString(stringListMap)));
            result= EncryptUtil.encrypt(Convert.toStr(mapper.writeValueAsString(stringListMap)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
